<?php
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
pc_base::load_app_func('util','content');
class lists {
	private $db;
	private $content_db;
	private $hits_db;
	function __construct() {
		$this->db = pc_base::load_model('content_model');
	}
	//首页
	public function init() {
		
		if(isset($_GET['siteid'])) {
			$siteid = intval($_GET['siteid']);
		} else {
			$siteid = 1;
		}
		$siteid = $GLOBALS['siteid'] = max($siteid,1);
		//SEO
		$SEO = seo($siteid);
		$sitelist  = getcache('sitelist','commons');
		$default_style = $sitelist[$siteid]['default_style'];	
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');
		include template('content','index',$default_style);
	}
	function l_case(){
		$catid = intval($_GET['catid']);
		$_priv_data = $this->_category_priv($catid);
		if($_priv_data=='-1') {
			$forward = urlencode(get_url());
			showmessage(L('login_website'),APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);
		} elseif($_priv_data=='-2') {
			showmessage(L('no_priv'));
		}
			
		if(!$catid) showmessage(L('category_not_exists'),'blank');
		$siteids = getcache('category_content','commons');
		$siteid = $siteids[$catid];
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');
		
		if(!isset($CATEGORYS[$catid])) showmessage(L('category_not_exists'),'blank');
		$CAT = $CATEGORYS[$catid];
	
		$siteid = $GLOBALS['siteid'] = $CAT['siteid'];
		extract($CAT);
		$setting = string2array($setting);
		
		//SEO
		if(!$setting['meta_title']) $setting['meta_title'] = $catname;
		$SEO = seo($siteid, '',$setting['meta_title'],$setting['meta_description'],$setting['meta_keywords']);
		define('STYLE',$setting['template_list']);
		$page = $_GET['page'];

		$template = $setting['category_template'] ? $setting['category_template'] : 'category';
		
		$template_list = $setting['list_template'] ? $setting['list_template'] : 'list';
	
		if($type==0) {
			
			$template = $child ? $template : $template_list;
			
			$arrparentid = explode(',', $arrparentid);
			$top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid;
			$array_child = array();
			$self_array = explode(',', $arrchildid);
			//获取一级栏目ids
			foreach ($self_array as $arr) {
				if($arr!=$catid && $CATEGORYS[$arr][parentid]==$catid) {
					$array_child[] = $arr;
				}
			}
			$arrchildid = implode(',', $array_child);
			//URL规则
//			$urlrules = getcache('urlrules','commons');
//			$urlrules = str_replace('|', '~',$urlrules[$category_ruleid]);
//			$tmp_urls = explode('~',$urlrules);
//			$tmp_urls = isset($tmp_urls[1]) ?  $tmp_urls[1] : $tmp_urls[0];
//			preg_match_all('/{\$([a-z0-9_]+)}/i',$tmp_urls,$_urls);
//			
//			if(!empty($_urls[1])) {
//				foreach($_urls[1] as $_v) {
//					$GLOBALS['URL_ARRAY'][$_v] = $_GET[$_v];
//				}
//			}
//			if($urlrules){
//				define('URLRULE', $urlrules);
//			}
			//echo $urlrules;exit;
			$GLOBALS['URL_ARRAY']['categorydir'] = $categorydir;
			$GLOBALS['URL_ARRAY']['catdir'] = $catdir;
			$GLOBALS['URL_ARRAY']['catid'] = $catid;
	
			$this->category = getcache('category_content_'.$siteid,'commons');
			//echo '<pre>';
			//print_r($this->category);exit;
			$this->modelid = $this->category[$catid]['modelid'];
			
			$this->content_db = pc_base::load_model('content_model');
			
			$tbpre = $this->content_db->db_tablepre;
			$page = $_GET['page']?$_GET['page']:1 ;
			$pagesize = 20;
			$offset = intval($pagesize*($page-1));
			
			$sp = intval($_GET['sp']);//空间
			$tp = intval($_GET['tp']);//户型 
			$st = intval($_GET['st']);//风格
			if($sp>0){
				$spc = ' b.decoration_space = '.$sp.' and ';
			}else{$spc = '1=1 and ';}
			
			if($tp>0){
				$tpc = ' b.house_type = '.$tp.' and ';
			}else{$tpc = '1=1 and ';}
			
			if($st>0){
				$stc = ' b.decoration_style= '.$st.'  ';
			}else{$stc = '1=1  ';}
			
			$con = $spc.$tpc.$stc;
			
			$sql = 'select a.*,b.project_cost,b.house_type  from '.$tbpre.'company_case as a 
			left join '.$tbpre.'company_case_data as b on a.id=b.id 
			where '.$con.' order by a.id desc';
			$sql .= " LIMIT $offset,$pagesize ";
		
			$this->content_db->query($sql);
			$message_data = $this->content_db->fetch_array();
			$sql_count = 'select count(a.id) as num  from '.$tbpre.'company_case as a 
			left join '.$tbpre.'company_case_data as b on a.id=b.id 
			where '.$con;
			$this->content_db->query($sql_count);
			$total = $this->content_db->fetch_array();
			$total = $total[0]['num'];
			$this->hits_db = pc_base::load_model('hits_model');
			for ($j=0;$j<count($message_data);$j++){
				$hitsid .= 'hitsid = \'c-'.$this->modelid.'-'.$message_data[$j]['id']['id'].'\' or ';
				
			}
			$sqlhitsid = substr($hitsid, 0, strlen($hitsid)-3);
			
			$hitsid = 'c-'.$this->modelid.'-%';
			$sqlhit = "hitsid LIKE '$hitsid'";
			$result = $this->hits_db->select($sqlhitsid, 'hitsid,views', $data['limit'],  'hitsid desc');
			//echo count($result);exit;
			for ($i=0;$i<count($result);$i++){
				$result[$i]['hitsid'] = str_replace('c-'.$this->modelid.'-', '', $result[$i]['hitsid']);
				if($result[$i]['hitsid'] == $message_data[$i]['id']){
				
					$array[$i] = array_merge($result[$i], $message_data[$i]);
				}
			}
			$casepages = pages($total, $page, $pagesize);
			include template('content',$template);
			} else {
				
			}
	}
	function product(){
		$catid = intval($_GET['catid']);
		$_priv_data = $this->_category_priv($catid);
		if($_priv_data=='-1') {
			$forward = urlencode(get_url());
			showmessage(L('login_website'),APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);
		} elseif($_priv_data=='-2') {
			showmessage(L('no_priv'));
		}
			
		if(!$catid) showmessage(L('category_not_exists'),'blank');
		$siteids = getcache('category_content','commons');
		$siteid = $siteids[$catid];
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');
		
		if(!isset($CATEGORYS[$catid])) showmessage(L('category_not_exists'),'blank');
		$CAT = $CATEGORYS[$catid];
	
		$siteid = $GLOBALS['siteid'] = $CAT['siteid'];
		extract($CAT);
		$setting = string2array($setting);
		
		//SEO
		if(!$setting['meta_title']) $setting['meta_title'] = $catname;
		$SEO = seo($siteid, '',$setting['meta_title'],$setting['meta_description'],$setting['meta_keywords']);
		define('STYLE',$setting['template_list']);
		$page = $_GET['page'];

		$template = $setting['category_template'] ? $setting['category_template'] : 'category';
		
		$template_list = $setting['list_template'] ? $setting['list_template'] : 'list';
	
		if($type==0) {
			
			$template = $child ? $template : $template_list;
			
			$arrparentid = explode(',', $arrparentid);
			$top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid;
			$array_child = array();
			$self_array = explode(',', $arrchildid);
			//获取一级栏目ids
			foreach ($self_array as $arr) {
				if($arr!=$catid && $CATEGORYS[$arr][parentid]==$catid) {
					$array_child[] = $arr;
				}
			}
			$arrchildid = implode(',', $array_child);
			//URL规则
//			$urlrules = getcache('urlrules','commons');
//			$urlrules = str_replace('|', '~',$urlrules[$category_ruleid]);
//			$tmp_urls = explode('~',$urlrules);
//			$tmp_urls = isset($tmp_urls[1]) ?  $tmp_urls[1] : $tmp_urls[0];
//			preg_match_all('/{\$([a-z0-9_]+)}/i',$tmp_urls,$_urls);
//			
//			if(!empty($_urls[1])) {
//				foreach($_urls[1] as $_v) {
//					$GLOBALS['URL_ARRAY'][$_v] = $_GET[$_v];
//				}
//			}
//			if($urlrules){
//				define('URLRULE', $urlrules);
//			}
			//echo $urlrules;exit;
			$GLOBALS['URL_ARRAY']['categorydir'] = $categorydir;
			$GLOBALS['URL_ARRAY']['catdir'] = $catdir;
			$GLOBALS['URL_ARRAY']['catid'] = $catid;
	
			$this->category = getcache('category_content_'.$siteid,'commons');
			//echo '<pre>';
			//print_r($this->category);exit;
			
			//取得建材品一级分类
			$product_cat = $this->get_product_cat();
			//
			if($_GET['cid']){
				$product_cat2['3341'] = $product_cat['3411'];
				$product_cat = $product_cat2;
			}
			
			//取得建材品二级分类
			$str = '';
			foreach ($product_cat as $key=>$val){
			
				$product_sub_cat = $this->get_product_cat($val['linkageid']);
				
				if($product_sub_cat){
					$i=1;
					$str .= '<div class="menu01"  id="tabcontent'.$i.'">';
					foreach ($product_sub_cat as $k=>$v){
						
						$str .= '<a href="'.APP_PATH.'index.php?m=content&c=lists&a=product&catid=18&mid='.$v['linkageid'].'" class="on">'.$v['name'].'</a>';
					}
					$str .= '</div>';
				}else{
					$str .= '<div class="menu01"  id="tabcontent'.$i.'"></div>';
				}
				$i++;
			}
			
			//
			
			$this->modelid = $this->category[$catid]['modelid'];
			
			$this->content_db = pc_base::load_model('content_model');
			
			$tbpre = $this->content_db->db_tablepre;
			$page = $_GET['page']?$_GET['page']:1 ;
			$pagesize = 20;
			$offset = intval($pagesize*($page-1));
			
			$cid = intval($_GET['cid']);//空间
			$mid = intval($_GET['mid']);
			
			if($cid>0){
				$cidc = ' b.maxproduct_category = '.$cid.' and  ';
			}else{$cidc = '1=1  and';}
			if($mid>0){
				$midc = ' b.minproduct_category = '.$mid.' ';
			}else{$midc = ' 1=1  ';}
			
			$con = $cidc.$midc;
			
			$sql = 'select a.*  from '.$tbpre.'product as a 
			left join '.$tbpre.'product_data as b on a.id=b.id 
			where '.$con.' order by a.id desc';
			$sql .= " LIMIT $offset,$pagesize ";
		
			$this->content_db->query($sql);
			$message_data = $this->content_db->fetch_array();
			$sql_count = 'select count(a.id) as num  from '.$tbpre.'product as a 
			left join '.$tbpre.'product_data as b on a.id=b.id 
			where '.$con;
			
			$this->content_db->query($sql_count);
			$total = $this->content_db->fetch_array();
			$total = $total[0]['num'];
			
			$this->hits_db = pc_base::load_model('hits_model');
			//echo $this->modelid;
			for ($j=0;$j<count($message_data);$j++){
				$hitsid .= 'hitsid = \'c-'.$this->modelid.'-'.$message_data[$j]['id'].'\' or ';
				
			}
			$sqlhitsid = substr($hitsid, 0, strlen($hitsid)-3);
		
			$hitsid = 'c-'.$this->modelid.'-%';
			$sqlhit = "hitsid LIKE '$hitsid'";
			
			$result = $this->hits_db->select($sqlhitsid, 'hitsid,views', $data['limit'],  'hitsid desc');
			//echo '<pre>';
			//print_r($result);
			//print_r($message_data);
			for ($i=0;$i<count($result);$i++){
				$result[$i]['hitsid'] = str_replace('c-'.$this->modelid.'-', '', $result[$i]['hitsid']);
				if($result[$i]['hitsid'] == $message_data[$i]['id']){
				//	echo __LINE__;
					$array[$i] = array_merge($result[$i], $message_data[$i]);
				}
			}
			$casepages = pages($total, $page, $pagesize);
			$template = 'list_photo';
			//print_r($array);exit;
			include template('content',$template,'product');
			} else {
				
			}
	}
	function zhaobiao(){
		$catid = intval($_GET['catid']);
		$_priv_data = $this->_category_priv($catid);
		if($_priv_data=='-1') {
			$forward = urlencode(get_url());
			showmessage(L('login_website'),APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);
		} elseif($_priv_data=='-2') {
			showmessage(L('no_priv'));
		}
			
		if(!$catid) showmessage(L('category_not_exists'),'blank');
		$siteids = getcache('category_content','commons');
		$siteid = $siteids[$catid];
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');
		
		if(!isset($CATEGORYS[$catid])) showmessage(L('category_not_exists'),'blank');
		$CAT = $CATEGORYS[$catid];
	
		$siteid = $GLOBALS['siteid'] = $CAT['siteid'];
		extract($CAT);
		$setting = string2array($setting);
		
		//SEO
		if(!$setting['meta_title']) $setting['meta_title'] = $catname;
		$SEO = seo($siteid, '',$setting['meta_title'],$setting['meta_description'],$setting['meta_keywords']);
		define('STYLE',$setting['template_list']);
		$page = $_GET['page'];

		$template = $setting['category_template'] ? $setting['category_template'] : 'category';
		
		$template_list = $setting['list_template'] ? $setting['list_template'] : 'list';
	
		if($type==0) {
			
			$template = $child ? $template : $template_list;
			
			$arrparentid = explode(',', $arrparentid);
			$top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid;
			$array_child = array();
			$self_array = explode(',', $arrchildid);
			//获取一级栏目ids
			foreach ($self_array as $arr) {
				if($arr!=$catid && $CATEGORYS[$arr][parentid]==$catid) {
					$array_child[] = $arr;
				}
			}
			$arrchildid = implode(',', $array_child);
			//URL规则
			$GLOBALS['URL_ARRAY']['categorydir'] = $categorydir;
			$GLOBALS['URL_ARRAY']['catdir'] = $catdir;
			$GLOBALS['URL_ARRAY']['catid'] = $catid;
	
			$this->category = getcache('category_content_'.$siteid,'commons');
			//echo '<pre>';
			//print_r($this->category);exit;
			
			//取得建材品一级分类
			$product_cat = $this->get_product_cat();
			//
			if($_GET['cid']){
				$product_cat2['3341'] = $product_cat['3411'];
				$product_cat = $product_cat2;
			}
			
			//取得建材品二级分类
			$str = '';
			foreach ($product_cat as $key=>$val){
			
				$product_sub_cat = $this->get_product_cat($val['linkageid']);
				
				if($product_sub_cat){
					$i=1;
					$str .= '<div class="menu01"  id="tabcontent'.$i.'">';
					foreach ($product_sub_cat as $k=>$v){
						
						$str .= '<a href="'.APP_PATH.'index.php?m=content&c=lists&a=product&catid=18&mid='.$v['linkageid'].'" class="on">'.$v['name'].'</a>';
					}
					$str .= '</div>';
				}else{
					$str .= '<div class="menu01"  id="tabcontent'.$i.'"></div>';
				}
				$i++;
			}
			
			
			$this->modelid = $this->category[$catid]['modelid'];
			
			$this->content_db = pc_base::load_model('content_model');
			
			$tbpre = $this->content_db->db_tablepre;
			$page = $_GET['page']?$_GET['page']:1 ;
			$pagesize = 20;
			$offset = intval($pagesize*($page-1));
			
			$areaid = intval($_GET['areaid']);//区域
			$hs = intval($_GET['hs']);//户型 
			$bg = $_GET['bg'];//预算
			$size = $_GET['size'];//面积
		
			
			if($areaid>0){
				$careaid = ' b.area = '.$areaid.' and  ';
			}else{$careaid = '1=1  and';}
			if($hs>0){
				$hsc = ' b.house_type = '.$hs.' and ';
			}else{$hsc = ' 1=1 and ';}
			if($bg){
				
				$bg = explode(",", $bg);
				$cbg = ' b.budget between '.(int)$bg[0].' and '.(int)$bg[1].' and ';
			}else{$cbg = ' 1=1 and ';}
			if($size){
				$size = explode(",", $size);
				$csize = ' b.size between '.(int)$size[0].' and '.(int)$size[1];
			}else{$csize = ' 1=1  ';}
			
			$con = $careaid.$hsc.$cbg.$csize.' and b.city ='.CITYID;
		
			$sql = 'select a.*,b.budget,b.size,b.name,b.decoration_space  from '.$tbpre.'zhaobiao as a 
			left join '.$tbpre.'zhaobiao_data as b on a.id=b.id 
			where '.$con.' order by a.id desc';
			$sql .= " LIMIT $offset,$pagesize ";

			$this->content_db->query($sql);
			$z_data = $this->content_db->fetch_array();
			$sql_count = 'select count(a.id) as num  from '.$tbpre.'zhaobiao as a 
			left join '.$tbpre.'zhaobiao_data as b on a.id=b.id 
			where '.$con;
			
			$this->content_db->query($sql_count);
			$total = $this->content_db->fetch_array();
			$total = $total[0]['num'];
			
			$this->hits_db = pc_base::load_model('hits_model');
			
			$casepages = pages($total, $page, $pagesize);
			$template = 'list';
			//print_r($z_data);exit;
			include template('content',$template,'zhaobiao');
			} else {
				
			}
	}
	/**
	 * 
	 * 取建材产品的的分类
	 */
	function get_product_cat($id){
		$cat = getcache('3410', 'linkage');
		
		$cats = $cat['data'];
		if($id>0){
			foreach ($cats as $k=>$v)
			{	
				if($v['parentid']!= $id){	
					unset($cats[$k]);
				}else{}	
			}		
			return $cats;
		}
		$data = $cat['data'];
		return $data;
	}
	function get_prodcut_category($id){
		$cities = getcache('3410', 'linkage');
		$data = $cities['data'];
		//$id = $param['id'];
		$l = $data;
		if($id!=0){
			if(is_array($data))
			{
				if($data[$k]['linkageid']==$id){
						$letter = $data[$k]['description'];
					}
					echo $data[$k]['linkageid'];
				foreach ($data as $k=>$v){
				
					if($v['parentid']!= $id || $v['parentid']==0){
						unset($data[$k]);
					}
					
				}	
				//$str .= '<option disabled style="background-color: rgb(238, 238, 238); color: rgb(255, 0, 0); font-weight: bold;">请先选择产品小类</option>';
				//foreach ($data as $key=>$val){
					
				//	$str .= '<option value="'.$val['linkageid'].'">'.$val['name'].'</option>';
				//}
			}	
		}
		else
		{
			$str .= '<option disabled style="background-color: rgb(238, 238, 238); color: rgb(255, 0, 0); font-weight: bold;">请先选择产品小类</option>';	
		}
		return $data;
	//	$data = array();
	//	$arr = array('str'=>$str,'letter'=>$l[$id]['description']);
	//	//print_r($arr);
	//	echo json_encode($arr);
	}
//取得文章的点击数
	function get_view(){
		$id = $_POST['id'];
		$hitsid = $_POST['hitsid'];
		$this->hits_db = pc_base::load_model('hits_model');
		$result = $this->hits_db->get_views($hitsid);
		$arr = array('id'=>$id, 'views'=>$result['views']);
		echo json_encode($arr);
	}
/**
	 * 检查阅读权限
	 *
	 */
	private function _category_priv($catid) {
		$catid = intval($catid);
		if(!$catid) return '-2';
		$_groupid = param::get_cookie('_groupid');
		$_groupid = intval($_groupid);
		if($_groupid==0) $_groupid = 8;
		$this->category_priv_db = pc_base::load_model('category_priv_model');
		$result = $this->category_priv_db->select(array('catid'=>$catid,'is_admin'=>0,'action'=>'visit'));
		if($result) {
			if(!$_groupid) return '-1';
			foreach($result as $r) {
				if($r['roleid'] == $_groupid) return '1';
			}
			return '-1';
		} else {
			return '1';
		}
	 }
	
}
?>